package com.wzz.common.widget;

import com.hymobile.common.R;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;
import android.widget.LinearLayout.LayoutParams;
/**
 * 底部加載數據listview
 * 需要文件listviewloading_main.xml
 * com.hymobile.common.widget.ListViewLoading
 * @author 祁元美 <br/>
 * create at 2011-11-22 下午2:48:50
 */
public class ListViewLoading extends Activity implements OnScrollListener {

  private listViewAdapter adapter = new listViewAdapter();  
  private ListView listView;   
  private LinearLayout loadingLayout; 
  private Thread mThread;
  private ProgressBar progressBar ;
  private LayoutParams mLayoutParams = new LinearLayout.LayoutParams(         
      LinearLayout.LayoutParams.WRAP_CONTENT,       
      LinearLayout.LayoutParams.WRAP_CONTENT);

  private LayoutParams FFlayoutParams = new LinearLayout.LayoutParams(        
      LinearLayout.LayoutParams.FILL_PARENT,        
      LinearLayout.LayoutParams.FILL_PARENT);

  public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.listviewloading_main);
    init();
  }  

  private void init() {    
    // TODO Auto-generated method stub    
    // 线性布局     
    LinearLayout layout = new LinearLayout(this);     
    // 设置布局 水平方向   
    layout.setOrientation(LinearLayout.HORIZONTAL);      
    // 进度条  
    progressBar = new ProgressBar(this);    
    // 进度条显示位置      
    progressBar.setPadding(0, 0, 15, 0);    
    // 把进度条加入到layout中      
    layout.addView(progressBar, mLayoutParams);    
    // 文本内容     
    TextView textView = new TextView(this);   
    textView.setText("加载中...");     
    textView.setGravity(Gravity.CENTER_VERTICAL);    
    // 把文本加入到layout中 
    layout.addView(textView, FFlayoutParams);    
    // 设置layout的重力方向，即对齐方式是   
    layout.setGravity(Gravity.CENTER);    
    // 设置ListView的页脚layout     
    loadingLayout = new LinearLayout(this);    
    loadingLayout.addView(layout, mLayoutParams);  
    loadingLayout.setGravity(Gravity.CENTER);       
    // 得到一个ListView用来显示条目     
    listView = (ListView) findViewById(R.id.tv);    
    // 添加到脚页显示  
    listView.addFooterView(loadingLayout);   
    // 给ListView添加适配器    
    listView.setAdapter(adapter);       
    // 给ListView注册滚动监听  
    listView.setOnScrollListener(this);    
  }

  public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    if(firstVisibleItem+visibleItemCount==totalItemCount){           
      //开线程去下载网络数据        
      if (mThread == null || !mThread.isAlive()) {
        mThread = new Thread() {
          public void run() {
            try {
              Thread.sleep(5000);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
            //handler
            handler.sendEmptyMessage(1);
          }
        };
        mThread.start();
      }

    }
  }

  private Handler handler = new Handler(){
    public void handleMessage(Message message){
      switch(message.what){
        case 1:
          if(adapter.count < 41){
            adapter.count += 10;
            int currentPage = adapter.count / 10;
            Toast.makeText(getApplicationContext(),
                "第" + currentPage + "页", Toast.LENGTH_LONG).show();
          }else{
            listView.removeFooterView(loadingLayout);
          }
          //重新刷新Listview的adapter里面数据        
          adapter.notifyDataSetChanged();
          break;
        case 2:
          break;
      }
    };

  };
  public void onScrollStateChanged(AbsListView view, int scrollState) {
  }

  class listViewAdapter extends BaseAdapter {
    int count = 10;
    public int getCount() {
      return count;
    }


    public Object getItem(int position) {
      return position;
    }


    public long getItemId(int position) {
      return position;
    }


    public View getView(int position, View v, ViewGroup parent) {
      TextView textView ;
      if(v == null){
        textView = new TextView(ListViewLoading.this);

      }else{           
        textView = (TextView) v;             
      }
      textView.setText("ListItem " + position);    
      textView.setTextSize(20f);          
      textView.setGravity(Gravity.CENTER);      
      textView.setHeight(60);      
      return textView;
    }
  }

}

